package com.bieyang.anjia.ui.activity;

import org.webrtc.mqttclientsample.LogUtis;
import org.webrtc.mqttclientsample.Message;
import org.webrtc.mqttclientsample.MessageService;

import com.bieyang.anjia.R;
import com.bieyang.anjia.module.LoginTypeEnum;
import com.bieyang.anjia.module.User;
import com.bieyang.anjia.net.NetManager;

import com.bieyang.anjia.utils.Constants;
import com.bieyang.anjia.utils.SharedPrefsUtil;
import com.bieyang.anjia.utils.Show;
import com.bieyang.anjia.utils.SubUtils;

import de.greenrobot.event.EventBus;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

public class AcChooseZhuji extends BaseFlingActivity {
	Button btnBack, btnSure;
	private Context con;
	protected static final int UPDATA = 0;
	protected static final int PUSHSERVICE_ISRUN = 1;
	protected static final int OK = -555;
	protected static final int MAIN_OK = -553;
	protected static final int SUB_OK = -552;

	public RadioGroup streamType;
	int stream;

	static String uuid;
	static String userName;
	static String passWord;
	MyServiceConnection conn;

	Intent intent;

	private Handler Uihandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constants.LOGIN_SUCESS:
				// Show.toast(con, R.string.tip_login_ok);
				// saveLoginInfo();
				PubService.isrun = true;
				Intent it = new Intent(con, PubService.class); // 开启服务
				startService(it);

				/*
				 * register call
				 */
				conn = new MyServiceConnection();
				Intent serviceIntent = new Intent(con, MessageService.class);
				bindService(serviceIntent, conn, BIND_AUTO_CREATE);

				registerEvent();
				MessageService.startService(con, SubUtils.getUuid(con));

				if (0 == stream) {
					App.isLogin = true;
					SharedPrefsUtil.putValue(App.mContext, "phone", userName);
					intent = new Intent(con, AcMain.class);
					startActivity(intent);

					finish();
				} else {
					SharedPrefsUtil.putValue(con, "subIsLogin", true);
					intent = new Intent(con, SubMain.class);
					startActivity(intent);
					finish();
				}

				break;
			case Constants.LOGIN_NO_USER: // no user
				Show.toast(con, R.string.tip_no_user);
				break;
			case Constants.LOGIN_WRONG_PASSWORD: // wrong password
				Show.toast(con, R.string.tip_wrong_password);
				break;
			case Constants.LOGIN_SUB_NO: // wrong password
				Show.toast(con, R.string.tip_sub_no);
				break;

			case Constants.LOGIN_FAIL:
				Show.toast(con, R.string.tip_login_fail);
				break;
			default:
				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_choose_zhuji);
		con = AcChooseZhuji.this;
		initView();
	}

	private void initView() {
		OnClick onClick = new OnClick();
		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(onClick);

		btnSure = (Button) findViewById(R.id.btnSure);
		btnSure.setOnClickListener(onClick);

		streamType = (RadioGroup) findViewById(R.id.stream_type);

	}

	class OnClick implements android.view.View.OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btnSure:

				if (streamType.getCheckedRadioButtonId() == R.id.maintype) {
					stream = 0;
				} else {
					stream = 1;
				}

				login();

				// if (0 == stream) {
				// startActivity(new Intent(con, AcLive.class));
				// } else {
				// startActivity(new Intent(con, SubMain.class));
				// }
				// LoginServer();
				break;

			default:
				break;
			}
			finish();
		}

	}


	void login() {

		uuid = SubUtils.getUuid(con);
		userName = SharedPrefsUtil.getStringValue(App.mContext, "phone", "");
		passWord = SharedPrefsUtil.getStringValue(App.mContext, "password", "");

		User user = new User();
		user.setCardNo(userName);
		user.setPassword(passWord);
		user.setUuid(uuid);

		if (0 == stream) {
			user.setType(LoginTypeEnum.MAIN);
			SharedPrefsUtil.putValue(App.mContext, "loginType", "M");
		} else {
			user.setType(LoginTypeEnum.CLIENT);
			SharedPrefsUtil.putValue(App.mContext, "loginType", "C");
		}
		new NetManager().login(con, user, Uihandler);

	}

	private class MyServiceConnection implements
			android.content.ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
		}

	}

	private void registerEvent() {
		EventBus.getDefault().register(this, "onMessage", Message.class);
	}

	public void onMessage(final Message message) {
		LogUtis.log("from:" + message.getFrom() + "," + "to:" + message.getTo()
				+ "," + "content:" + message.getContent() + "," + "type:"
				+ message.getType());
		switch (message.getType()) {

		case PHONE:

			break;

		default:
			break;
		}
	}

}
